<?php
/*==========================================================================*\
| AGRIVIET - CONTENT MANAGERMENT SYSTEM						
\*==========================================================================*/

function resize_image($pathStart,$pathFinal,$maxwidth,$maxheight,$quality)
{
	$sExtension = substr( $pathStart, ( strrpos($pathStart, '.') + 1 ) ) ;
	$sExtension = strtolower($sExtension) ;

	// Get new dimensions
	list($width, $height) = getimagesize($pathStart);
	if ($width != 0 && $height !=0)
	{
		if ($maxwidth / $width > $maxheight / $height)
		{
			$percent = $maxheight / $height;
			if($height<$maxheight) $percent=1;
		}
		else
		{
			$percent = $maxwidth / $width;
			if($width<$maxwidth) $percent=1;
		}
	}

		
	
	$new_width = $width * $percent;
	$new_height = $height * $percent;
	
	// Resample
	$image_p = imagecreatetruecolor($new_width, $new_height);
	//check extension file
	switch ($sExtension){
	case "jpg" :
	case "jpeg" :
		$image = imagecreatefromjpeg($pathStart);
		break;
	case "gif" :
		$image = imagecreatefromgif($pathStart);
		break;
	case "png" :
		$image = imagecreatefrompng($pathStart);
		break;	
	}
	//$image = imagecreatefromjpeg($path . $filename);
	imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
	
	// Output
	
	switch ($sExtension){
	case "jpg" :
	case "jpeg" :
		//imagejpeg($image_p, $path . $filename, $quality);
		//imagejpeg($image_p, $path . "small_" . $filename, $quality);
		imagejpeg($image_p, $pathFinal, $quality);
		break;
	case "gif" :
		imagegif($image_p, $pathFinal);
		//imagegif($image_p, $path . "small_" . $filename);
		break;
	case "png" :
		imagepng($image_p, $pathFinal);
		//imagegif($image_p, $path . "small_" . $filename);
		break;	
	}
	//imagejpeg($image_p, $path . "small_" . $filename, $quality);
	imagedestroy($image_p);
}
?>